package web;
import java.awt.Color;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhuozhengsoft.pageoffice.DocumentOpenType;
import com.zhuozhengsoft.pageoffice.FileMakerCtrl;
import com.zhuozhengsoft.pageoffice.wordwriter.DataRegion;
import com.zhuozhengsoft.pageoffice.wordwriter.DataRegionInsertType;
import com.zhuozhengsoft.pageoffice.wordwriter.Font;
import com.zhuozhengsoft.pageoffice.wordwriter.ParagraphFormat;
import com.zhuozhengsoft.pageoffice.wordwriter.Table;
import com.zhuozhengsoft.pageoffice.wordwriter.WdAutoFitBehavior;
import com.zhuozhengsoft.pageoffice.wordwriter.WdBorderType;
import com.zhuozhengsoft.pageoffice.wordwriter.WdLineSpacing;
import com.zhuozhengsoft.pageoffice.wordwriter.WdLineWidth;
import com.zhuozhengsoft.pageoffice.wordwriter.WdParagraphAlignment;
import com.zhuozhengsoft.pageoffice.wordwriter.WdRowHeightRule;
import com.zhuozhengsoft.pageoffice.wordwriter.WordDocument;

@Controller
public class CreateWord extends HttpServlet{
	private static final long serialVersionUID = -758686623642845302L;
	
	@RequestMapping("createWord")  
    public String openword(HttpServletRequest request, HttpServletResponse response){
		FileMakerCtrl fmCtrl = new FileMakerCtrl(request);
		fmCtrl.setServerPage(request.getContextPath() + "/poserver.zz");
		WordDocument doc = new WordDocument();

		int index = 0;
		for(int i = 0; i < 3; i++){
			index++;
			//设置内容标题
			//创建DataRegion对象，PO_title为自动添加的书签名称,书签名称需以“PO_”为前缀，切书签名称不能重复
			//三个参数分别为要新插入书签的名称、新书签的插入位置、相关联的书签名称（“[home]”代表Word文档的第一个位置）
			DataRegion organName = null;
			if(index == 1){
				organName = doc.createDataRegion("PO_organName"+index,DataRegionInsertType.After, "[home]");
			}else{
				organName = doc.createDataRegion("PO_organName"+index,DataRegionInsertType.After, "[end]");
			}
			//给DataRegion对象赋值
			organName.setValue("深圳市社会保险基金管理局\n");
			//设置字体：粗细、大小、字体名称、是否是斜体
			organName.getFont().setBold(true);
			organName.getFont().setSize(8);
			organName.getFont().setName("宋体");
			organName.getFont().setItalic(false);
			//定义段落对象
			ParagraphFormat titlePara = organName.getParagraphFormat();
			//设置段落对齐方式
			titlePara.setAlignment(WdParagraphAlignment.wdAlignParagraphCenter);
			//设置段落行间距
			titlePara.setLineSpacingRule(WdLineSpacing.wdLineSpaceMultiple);

			DataRegion docName = doc.createDataRegion("PO_docName"+index,DataRegionInsertType.After, "PO_organName"+index);
			//给DataRegion对象赋值
			docName.setValue("社会医疗保险医疗费用报销单\n");
			//设置字体：粗细、大小、字体名称、是否是斜体
			docName.getFont().setBold(false);
			docName.getFont().setSize(12);
			docName.getFont().setName("微软雅黑");
			docName.getFont().setItalic(false);
			//定义段落对象
			ParagraphFormat titlePara2 = docName.getParagraphFormat();
			//设置段落对齐方式
			titlePara2.setAlignment(WdParagraphAlignment.wdAlignParagraphCenter);
			//设置段落行间距
			titlePara2.setLineSpacingRule(WdLineSpacing.wdLineSpace1pt5);
			
			DataRegion date = doc.createDataRegion("PO_date"+index,DataRegionInsertType.After, "PO_docName"+index);
			//给DataRegion对象赋值
			date.setValue("（市外定点）（报销年月：201805）\n");
			//设置字体：粗细、大小、字体名称、是否是斜体
			date.getFont().setBold(false);
			date.getFont().setSize(9);
			date.getFont().setName("微软雅黑");
			date.getFont().setItalic(false);
			//定义段落对象
			ParagraphFormat titlePara3 = date.getParagraphFormat();
			//设置段落对齐方式
			titlePara3.setAlignment(WdParagraphAlignment.wdAlignParagraphCenter);
			//设置段落行间距
			titlePara3.setLineSpacingRule(WdLineSpacing.wdLineSpaceMultiple);
			
			//设置内容
			//第一段
			//创建DataRegion对象，PO_body为自动添加的书签名称
			DataRegion body = doc.createDataRegion("PO_body"+index, DataRegionInsertType.After, "PO_date"+index);
			//创建表格
			Table table1 = body.createTable(18, 4, WdAutoFitBehavior.wdAutoFitFixed);
			//设置边框样式
			table1.getBorder().setBorderType(WdBorderType.wdNone);
			//设置表格行高
			table1.setRowsHeight(12,WdRowHeightRule.wdRowHeightAuto);
			//设置表格宽
			//table1.setPreferredWidthType(WdPreferredWidthType.wdPreferredWidthPercent);
			//table1.setPreferredWidth(0.8F);
			
			//设置表格字体样式
			Font font=body.getFont();
			//设置字体的是否加粗
			//font.setBold(false);
			//设置字体的颜色
			font.setColor(Color.black);
			//设置字体是否为斜体
			font.setItalic(false);
		       //设置字体名称
			font.setName("宋体");
			//设置字体大小
			font.setSize(11f);
			//定义段落对象
			ParagraphFormat titlePara4 = body.getParagraphFormat();
			//设置段落对齐方式
			titlePara4.setAlignment(WdParagraphAlignment.wdAlignParagraphLeft);
			
			//设置单元格的值
			table1.openCellRC(1, 1).setValue("电脑号：88009090");
			table1.openCellRC(1, 2).setValue("姓名：熊超");
			table1.openCellRC(1, 3).setValue("性别：男");
			table1.openCellRC(1, 4).setValue("年龄：36");
			
			table1.openCellRC(2, 1).setValue("参保类型：一档");
			table1.openCellRC(2, 2).setValue("单位编码：88009090");
			table1.openCellRC(2, 3).mergeTo(2, 4);
			table1.openCellRC(2, 3).setValue("单位名称：万达信息");
			
			table1.openCellRC(3, 1).mergeTo(3, 2);
			table1.openCellRC(3, 1).setValue("病情诊断：无病");
			table1.openCellRC(3, 3).mergeTo(3, 4);
			table1.openCellRC(3, 2).setValue("就诊医院：港大医院");
			
			table1.openCellRC(4, 1).setValue("费用日期：20180901");
			table1.openCellRC(4, 2).setValue("流水号：88009090");
			table1.openCellRC(4, 3).mergeTo(4, 4);
			table1.openCellRC(4, 3).setValue("（文书号：12412412414）");
			
			table1.openCellRC(5, 1).setValue("医保结算项目");
			table1.openCellRC(5, 1).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(5, 1).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			table1.openCellRC(5, 2).setValue("金额（元）");
			table1.openCellRC(5, 2).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(5, 2).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			table1.openCellRC(5, 3).mergeTo(5, 4);
			table1.openCellRC(5, 3).setValue("记账支付：¥ 4444");
			table1.openCellRC(5, 3).getFont().setBold(true);
			table1.openCellRC(5, 3).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(5, 3).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			
			table1.openCellRC(6, 1).mergeTo(6, 2);
			table1.openCellRC(7, 1).mergeTo(7, 2);
			table1.openCellRC(8, 1).mergeTo(8, 2);
			table1.openCellRC(6, 1).mergeTo(7, 1);
			table1.openCellRC(6, 1).mergeTo(8, 1);
			table1.openCellRC(6, 1).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(6, 1).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			table1.openCellRC(6, 2).mergeTo(6, 3);
			table1.openCellRC(6, 2).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(6, 2).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			table1.openCellRC(7, 2).mergeTo(7, 3);
			table1.openCellRC(7, 2).setValue("现金支付：¥ 4444");
			table1.openCellRC(7, 2).getFont().setBold(true);
			table1.openCellRC(7, 2).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(7, 2).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			table1.openCellRC(8, 2).mergeTo(8, 3);
			table1.openCellRC(8, 2).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(8, 2).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			
			table1.openCellRC(9, 1).mergeTo(9, 2);
			table1.openCellRC(9, 1).mergeTo(9, 3);
			table1.openCellRC(9, 1).mergeTo(9, 4);
			table1.openCellRC(9, 1).setValue("实报金额：（12354.00）元       （大写）（壹万贰仟叁佰伍拾肆圆整）");
			table1.openCellRC(9, 1).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(9, 1).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			
			table1.openCellRC(10, 1).mergeTo(10, 2);
			table1.openCellRC(10, 1).setValue("个人账户余额：");
			table1.openCellRC(10, 1).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(10, 1).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			table1.openCellRC(10, 2).setValue("基本医疗限额：");
			table1.openCellRC(10, 2).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(10, 2).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			table1.openCellRC(10, 3).setValue("补充医疗限额：");
			table1.openCellRC(10, 3).getBorder().setBorderType(WdBorderType.wdAllEdges);
			table1.openCellRC(10, 3).getBorder().setLineWidth(WdLineWidth.wdLineWidth050pt);
			
			table1.openCellRC(12, 1).mergeTo(12, 2);
			table1.openCellRC(12, 1).setValue("初审人：");
			table1.openCellRC(12, 2).setValue("复审人：");
			table1.openCellRC(12, 3).setValue("批准人：");
			
			table1.openCellRC(14, 1).mergeTo(14, 2);
			table1.openCellRC(14, 1).mergeTo(14, 3);
			table1.openCellRC(14, 1).mergeTo(14, 4);
			table1.openCellRC(14, 1).setValue("温馨提示：您参加了本年度深圳市重特大疾病补充医疗保险，本次结报可享受相应待遇，"
					+ "详见《深圳市重特大疾病补充医疗保险结报清单》。（上表“统筹支付”、“现金支付”栏勿将“重疾支付”及“冲重疾支付”项目显示。"
					+ "另外，如经办系统计算结果中含“重疾支付”项目时，该温馨提示才显示在报销单上，无“重疾支付”项目时，该提示不显示。）");
			
			table1.openCellRC(16, 1).mergeTo(16, 2);
			table1.openCellRC(16, 1).mergeTo(16, 3);
			table1.openCellRC(16, 1).mergeTo(16, 4);
			table1.openCellRC(16, 1).setValue("收款人：张三");
			
			table1.openCellRC(17, 1).mergeTo(17, 2);
			table1.openCellRC(17, 1).mergeTo(17, 3);
			table1.openCellRC(17, 1).mergeTo(17, 4);
			table1.openCellRC(17, 1).setValue("银行名称（编码）：中国银行（BK01）");
			
			table1.openCellRC(18, 1).mergeTo(18, 2);
			table1.openCellRC(18, 1).mergeTo(18, 3);
			table1.openCellRC(18, 1).mergeTo(18, 4);
			table1.openCellRC(18, 1).setValue("银行账号：6214677200019869689123456");
			if(i < 2){
				DataRegion docEnd = doc.createDataRegion("PO_first"+index, DataRegionInsertType.After, "[end]");
				docEnd.selectEnd();
				doc.insertPageBreak();//插入分页符
			}
		}
		
		fmCtrl.setSaveFilePage("savefile.do");
		fmCtrl.setWriter(doc);
		fmCtrl.setJsFunction_OnProgressComplete("OnProgressComplete()");
		fmCtrl.fillDocumentAsPDF("doc/blank.doc", DocumentOpenType.Word,"blank.pdf");
		fmCtrl.setTagId("FileMakerCtrl1");
		return "wordToPDF2";
    } 
}
